package com.siso.edu.instrument.util;

import android.content.Context;
import android.os.Environment;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Toast;

import java.io.File;

/**
 * Created by Administrator on 2017/5/11.
 */
public class SDCardUtil {

    private static final String TAG ="SDCardUtil" ;

    /**
     * SDCard存储空间是否大于200M
     * @param mContext
     * @return
     */
    public static boolean sdCardSize200M(Context mContext) {
        //使用前需要判断sdcard状态
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //sdcard状态是没有挂载的情况
            Toast.makeText(mContext, "sdcard不存在或未挂载", Toast.LENGTH_SHORT).show();
            return false;
        }
        //需要判断sdcard剩余空间
        //判断sdcard存储空间是否满足文件的存储
        File sdcard_filedir = Environment.getExternalStorageDirectory();//得到sdcard的目录作为一个文件对象
        long usableSpace = sdcard_filedir.getUsableSpace();//获取文件目录对象剩余空间
        long totalSpace = sdcard_filedir.getTotalSpace();
        Log.e(TAG, "sdCardSize:usableSpace= " +usableSpace);
        Log.e(TAG, "sdCardSize:totalSpace= " +totalSpace);
        //将一个long类型的文件大小格式化成用户可以看懂的M，G字符串
        String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
        String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
        Log.e(TAG, "sdCardSize:usableSpace_str= " +usableSpace_str);
        Log.e(TAG, "sdCardSize:totalSpace_str= " +totalSpace_str);
        if (usableSpace < 1024 * 1024 * 200) {//判断剩余空间是否小于200M
            Toast.makeText(mContext, "sdcard剩余空间不足,无法满足下载；剩余空间为：" + usableSpace_str, Toast.LENGTH_SHORT).show();
            return  false;
        }
        return true;
    }


}
